package com.appbee.media.server;

import java.io.IOException;
import java.util.Map;
import java.util.logging.Logger;

import javax.inject.Singleton;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.appbee.core.server.twig.CustomObjectDatastore;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;

@Singleton
public class ImageUploadService extends HttpServlet
{
	private static final Logger log = Logger.getLogger(ImageUploadService.class.getName());
	private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
	
//	@Inject
//	public ImageUploadService(BlobstoreService blobstoreService,Provider<PhotoDAO> photoDaoProvider)
//	{
//		this.blobstoreService = blobstoreService;
//		this.photoDaoProvider = photoDaoProvider;
//	}
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
	{
        log.info("upload get");
		resp.getWriter().println("ok");
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
	{
        log.info("upload");
		Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
        BlobKey blobKey = blobs.get("myFile");
        log.info("Blob key: "+blobKey.getKeyString());
        Media photo = new Media();
        photo.setStrId(blobKey.getKeyString());
        MediaDAO dao = new MediaDAO();
        dao.setObjectDatastore(new CustomObjectDatastore());
        dao.save(photo);
        log.info("Photo id: "+photo.getId());
        resp.sendRedirect("/blobservice/MediaId.jsp?id="+photo.getId());


	}

}
